package com.cocostudio.erpapp;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.widget.Toast;

import com.cocostudio.erpapp.db.DBHelper;
import com.cocostudio.erpapp.db.Tables;
import com.cocostudio.erpapp.provider.Settings;
import com.cocostudio.erpapp.vo.SaveResult;
import com.cocostudio.erpapp.web.Apis;
import com.cocostudio.erpapp.web.Keys;
import com.cocostudio.erpapp.web.ServerRequester;
import com.cocostudio.erpapp.web.ServerRequester.Parameters;
import com.cocostudio.erpapp.web.ServerRequester.Result;
import com.cocostudio.erpapp.R;

public final class SaveAction {

    private static OnSaveCompletionListener l;

    public static void save(Activity context, String type, String fid, OnSaveCompletionListener listener) {
        l = listener;
        if (Settings.checkLoginStatus(context)) {
            Parameters params = new Parameters(Apis.ACT_FAVORITE);
            params.put(Keys.UID, Settings.getUID(context));
            params.put(Keys.TYPE, type);
            params.put(Keys.FID, fid);
            new ActionWorker(context).execute(params);
        }
    }
    
    //通过调用异步存储机制，进行多线程存储操作，同时可以传递参数
    static class ActionWorker extends AsyncTask<Parameters, Void, SaveResult> {
        Context c;

        ActionWorker(Context context) {
            c = context;
        }

        @Override
        protected SaveResult doInBackground(Parameters... params) {
            Result result = ServerRequester.requestByGet(Apis.API_MY_EXHIBITIONS, params[0]);
            if (result.isOk()) {
                String type = params[0].getValue(Keys.TYPE);
                String fid = params[0].getValue(Keys.FID);
                SQLiteDatabase db = DBHelper.openDatabase(c, true);
                ContentValues values = new ContentValues();
                values.put(Keys.SAVED, 1);
                if (Apis.ACT_MY_SCHEDULES.equals(type)) {
                    db.update(Tables.SCHEDULE, values, Keys.SID + "=?", new String[] { fid });
                } else if (Apis.ACT_MY_EXHIBITORS.equals(type)) {
                    db.update(Tables.EXHIBITOR, values, Keys.ID + "=?", new String[] { fid });
                } else if (Apis.ACT_MY_PRODUCTS.equals(type)) {
                    db.update(Tables.PRODUCT, values, Keys.ID + "=?", new String[] { fid });
                } else if (Apis.ACT_MY_SPEECHS.equals(type)) {
                    db.update(Tables.SPEECH, values, Keys.SSID + "=?", new String[] { fid });
                }
            }
            return result.isOk() ? (SaveResult) result.vo : null;
        }

        @Override
        protected void onPostExecute(SaveResult result) {
            if (result != null) {
                if (result.status > 0) {
                    if (l != null) {
                        l.onSaveCompletion(true);
                    }
                    Toast.makeText(c, "发送成功", Toast.LENGTH_SHORT).show();
                } else {
                    if (l != null) {
                        l.onSaveCompletion(false);
                    }
                    Toast.makeText(c, result.msg, Toast.LENGTH_LONG).show();
                }
            } else {
                if (l != null) {
                    l.onSaveCompletion(false);
                }
                Toast.makeText(c, "发送失败", Toast.LENGTH_LONG).show();
            }

        }
    }

    public static interface OnSaveCompletionListener {
        public void onSaveCompletion(boolean success);
    }
}
